<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:isc="http://extension-functions.intersystems.com" xmlns:hl7="urn:hl7-org:v3" xmlns:sdtc="urn:hl7-org:sdtc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:exsl="http://exslt.org/common" exclude-result-prefixes="isc hl7 sdtc xsi exsl">
	<xsl:output method="xml" omit-xml-declaration="yes" indent="no" encoding="UTF-8"/>

	
	<!-- 引用全局模板 -->
	<xsl:include href="HCC-Support-Files/System/Templates/TemplateIdentifiers-HCC.xsl"/>
	<!--<xsl:include href="HCC-Support-Files/System/OIDs/OIDs-InterSystems.xsl"/>
	<xsl:include href="HCC-Support-Files/System/OIDs/OIDs-Other.xsl"/>-->
	<xsl:include href="HCC-Support-Files/System/Common/String-Functions.xsl"/>
	<xsl:include href="HCC-Support-Files/System/Common/Functions.xsl"/>
	<xsl:include href="HCC-Support-Files/Import/Common/Functions.xsl"/>
	<xsl:include href="HCC-Support-Files/Import/Common/Variables.xsl"/>
	<xsl:include href="HCC-Support-Files/Site/ImportProfile.xsl"/>
	<xsl:include href="HCC-Support-Files/Site/Variables.xsl"/>
	<!-- 引用章節模板 -->
	<xsl:include href="HCC-Support-Files/Import/Section-Modules/Encounters.xsl"/>
	<xsl:include href="HCC-Support-Files/Import/Section-Modules/ReasonForVisit.xsl"/>
	<xsl:include href="HCC-Support-Files/Import/Section-Modules/AllDiagnosisSections.xsl"/>
	<xsl:include href="HCC-Support-Files/Import/Section-Modules/AllObservationSections.xsl"/>
	<xsl:include href="HCC-Support-Files/Import/Section-Modules/AllProcedureSections.xsl"/>
	<xsl:include href="HCC-Support-Files/Import/Section-Modules/AllMedicationsSections.xsl"/>
	<xsl:include href="HCC-Support-Files/Import/Section-Modules/AllLabOrdersSections.xsl"/>
	<xsl:include href="HCC-Support-Files/Import/Section-Modules/AllRadOrdersSections.xsl"/>
	<xsl:include href="HCC-Support-Files/Import/Section-Modules/AllProblemsSections.xsl"/>
	<xsl:include href="HCC-Support-Files/Import/Section-Modules/AllSocialDeterminantSections.xsl"/>
	<xsl:include href="HCC-Support-Files/Import/Section-Modules/AllFamilyHistorySections.xsl"/>
	<xsl:include href="HCC-Support-Files/Import/Section-Modules/AllAllergySections.xsl"/>
	<xsl:include href="HCC-Support-Files/Import/Section-Modules/AllSocialHistorySections.xsl"/>
	<xsl:include href="HCC-Support-Files/Import/Section-Modules/AllVaccinationSections.xsl"/>
	<xsl:include href="HCC-Support-Files/Import/Section-Modules/AllOtherOrdersSections.xsl"/>
	<xsl:include href="HCC-Support-Files/Import/Section-Modules/AllAlertSections.xsl"/>
	<!-- 引用条目模板 -->
	<xsl:include href="HCC-Support-Files/Import/Entry-Modules/Encounter.xsl"/>
	<xsl:include href="HCC-Support-Files/Import/Entry-Modules/PersonalInformation.xsl"/>


	<!-- 组装 SDA 输出 -->
	<xsl:template match="/">
		<xsl:variable name="firstPass"><xsl:apply-templates select="/hl7:ClinicalDocument"/></xsl:variable>
		<xsl:apply-templates select="exsl:node-set($firstPass)" mode= "Canonicalize"/>
	</xsl:template>

	<xsl:template match="/hl7:ClinicalDocument">
		<Container>
			<!-- 发送机构 -->
			<!--<xsl:variable name="sendingFacility"><xsl:apply-templates select="." mode="SendingFacilityValue"/></xsl:variable>
			<xsl:if test="string-length($sendingFacility)">
				<SendingFacility><xsl:value-of select="$sendingFacility"/></SendingFacility>
			</xsl:if>-->
			<xsl:variable name="sendingFacility"><xsl:value-of select="/hl7:ClinicalDocument/hl7:custodian/hl7:assignedCustodian/hl7:representedCustodianOrganization/hl7:id/@extension"/></xsl:variable>
			<xsl:if test="string-length($sendingFacility)">
				<SendingFacility><xsl:value-of select="$sendingFacility"/></SendingFacility>
			</xsl:if>

			<Patient>
				<!-- 患者信息 -->
				<xsl:apply-templates select="hl7:recordTarget/hl7:patientRole" mode="PersonalInformation"/>
				
				<!-- Healthcare Provider module -->
				<xsl:apply-templates select="hl7:documentationOf" mode="HealthcareProvider"/>
				
			</Patient>
			
			<!-- 就诊信息-->	 
			<xsl:apply-templates select="." mode="Encounters"/>
								
			<!-- 诊断相关： 入院诊断章节, 出院诊断章节, 诊断记录章节, 评估 -->
			<Diagnoses>
				<xsl:apply-templates select="." mode="HospitalDischargeDXSection"/>
				<xsl:apply-templates select="." mode="DiagnosesSection"/>
				<xsl:apply-templates select="." mode="AdmissionDiagnosesSection"/>	
				<xsl:apply-templates select="." mode="SurgicalOperationNotePostoperativeDxSection"/>
				<xsl:apply-templates select="." mode="SurgicalOperationNotePreoperativeDxSection"/>
				<xsl:apply-templates select="." mode="DeathReasonSection"/>		
				<xsl:apply-templates select="." mode="HealthcareEventDXSection"/>
				<xsl:apply-templates select="." mode="HealthcareEventDDXSection"/>				
				<xsl:apply-templates select="." mode="ProblemListDXSection"/>	
			</Diagnoses>

			<Observations>
				<xsl:apply-templates select="." mode="DeliveryAssessmentNoteSection"/>
				<xsl:apply-templates select="." mode="PostpartumHospitalizationTreatmentSection"/>
				<xsl:apply-templates select="." mode="PrenatalEventsSection"/>
				<xsl:apply-templates select="." mode="NursingSignSection"/>
				<xsl:apply-templates select="." mode="HealthInstructionsSection"/>
				<xsl:apply-templates select="." mode="NursingObservationsSection"/>
				<xsl:apply-templates select="." mode="IntegumentarySystemSection"/>
				<xsl:apply-templates select="." mode="BloodLossSection"/>
				<xsl:apply-templates select="." mode="ExtremitiesSection"/>
				<xsl:apply-templates select="." mode="NewbornSection"/>
				<xsl:apply-templates select="." mode="GeneralStatusSection"/>
				<xsl:apply-templates select="." mode="MenstrualHistorySection"/>
				<xsl:apply-templates select="." mode="HistoryOfHospitalizationsSection"/>
				<xsl:apply-templates select="." mode="LaborAndDeliveryProcessSection"/>
				<xsl:apply-templates select="." mode="VitalSignSection"/>
				<xsl:apply-templates select="." mode="SurgicalOperationNoteAnesthesiaSection"/>
				<xsl:apply-templates select="." mode="PhysicalExaminationSection"/>					
			</Observations>
		
			<Procedures>
				<xsl:apply-templates select="." mode="HistoryOfProceduresSection"/>
				<xsl:apply-templates select="." mode="NursingOperationsSection"/>
				<xsl:apply-templates select="." mode="OtherOperationsSection"/>
				<xsl:apply-templates select="." mode="HistoryOfBloodTransfusionSection"/>
				<xsl:apply-templates select="." mode="RescueRecordSection"/>
				<xsl:apply-templates select="." mode="SurgicalDrainsSection"/>	
				<xsl:apply-templates select="." mode="HealthcareEventOperationsSection"/>					
			</Procedures>			
			
			<Medications>
				<xsl:apply-templates select="." mode="HistoryOfMedicationUseSection"/>
				<xsl:apply-templates select="." mode="MedicationAdministeredSection"/>
				<xsl:apply-templates select="." mode="ProviderMedOrdersSection"/>
				<xsl:apply-templates select="." mode="HealthcareEventMedSection"/>
			</Medications> 

			<LabOrders>
				<xsl:apply-templates select="." mode="LabStudiesSummarySection"/>
			</LabOrders>

			<RadOrders>
				<xsl:apply-templates select="." mode="ProviderRabOrdersSection"/>
				<xsl:apply-templates select="." mode="RadPhysicalExaminationSection"/>
			</RadOrders>	

			<Problems>
				<xsl:apply-templates select="." mode="HistoryOfPastIllnessSection"/>
				<xsl:apply-templates select="." mode="HistoryOfPresentIllnessSection"/>
				<xsl:apply-templates select="." mode="ProblemListSection"/>
			</Problems>
					
			<SocialDeterminants>
				<xsl:apply-templates select="." mode="HCCSocialHistorySection"/>
			</SocialDeterminants>
			 
			<FamilyHistories>
				<xsl:apply-templates select="." mode="FamilyHistorySection"/>
			</FamilyHistories>			
			 
			<Allergies>
				<xsl:apply-templates select="." mode="AllergiesSection"/>
				<xsl:apply-templates select="." mode="AllergiesOfPastIllnessSection"/>
			</Allergies>			  
			  
			<SocialHistories>
				<xsl:apply-templates select="." mode="SocialDeterminantSection"/>
			</SocialHistories>
				
			<!-- Vaccinations 转换FHIR报错:<THROW>zHandleInvalidResource+4^HS.FHIR.DTL.Util.API.Transform.SDA3ToFHIR.1 *%Exception.StatusException ERROR <HSFHIRErr>ValidationFailure: Details follow
ERROR <HSFHIRErr>MissingRequiredProperty: The type 'Immunization' requires a property named 'occurrence'
ERROR <HSFHIRErr>MissingRequiredProperty: The type 'Immunization' requires a property named 'status'-->
			<!--<Vaccinations>
				<xsl:apply-templates select="." mode="VaccinationSection"/>
			</Vaccinations>-->
			
			<OtherOrders>
				<xsl:apply-templates select="." mode="HighValueConsumableSection"/>
				<xsl:apply-templates select="." mode="DischargeDxOrderSection"/>
			</OtherOrders>
			
			<Alerts>
				<xsl:apply-templates select="." mode="RiskSection"/>
				<xsl:apply-templates select="." mode="NoticesSection"/>
				<xsl:apply-templates select="." mode="EducationNoteSection"/>
			</Alerts>
		
			</Container>
	</xsl:template>

	<!-- At the top SDA level, insert the first element, then loop through the node sets of subsequent elements with the same name -->
	<xsl:template match="/Container/*" mode="Canonicalize"> 
		<xsl:variable name="elementName" select="local-name()"/>
		<xsl:if test="count(preceding-sibling::*[name()=$elementName])=0">
				<xsl:copy>
					<xsl:for-each select="self::* | following-sibling::*[name()=$elementName]">
						<xsl:apply-templates mode="Canonicalize"/> 
					</xsl:for-each>
				</xsl:copy>
		</xsl:if>
	</xsl:template>

	<!-- Copy all other SDA elements as is -->
	<xsl:template match="*" mode="Canonicalize"> 
		<xsl:copy>
			<xsl:apply-templates mode="Canonicalize"/> 
		</xsl:copy>
	</xsl:template>
	
	<!--
		AdditionalDocumentInfo reconstitutes selected CDA XML element and attribute data
		into an XML structure that is purely elements and values, with no attributes.
		This is done so that the data survives the final template calls to Canonicalize.
	-->
	<xsl:template match="hl7:ClinicalDocument" mode="AdditionalDocumentInfo">
		<AdditionalDocumentInfo>
			<SourceFormat>HCC</SourceFormat>
			<xsl:apply-templates select="." mode="fn-DocumentHeaderInfo"/>
			<xsl:apply-templates select="." mode="ImportCustom-AdditionalDocumentInfo"/>
		</AdditionalDocumentInfo>
	</xsl:template>
	
	<!-- This empty template may be overridden with custom logic. -->
	<xsl:template match="*" mode="ImportCustom-Container">
	</xsl:template>
	
	<!-- This empty template may be overridden with custom logic. -->
	<xsl:template match="*" mode="ImportCustom-AdditionalDocumentInfo">
	</xsl:template>
</xsl:stylesheet>
